home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / gwuada_9.zip / READ.C < prev    next >
C/C++ Source or Header  |  1993-07-27  |  25KB  |  810 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9.  
  10. #define GEN
  11.  
  12. #include "hdr.h"
  13. #include "libhdr.h"
  14. #include "vars.h"
  15. #include "segment.h"
  16. #include "gvars.h"
  17. #include "slot.h"
  18. #include "ifile.h"
  19. #include "axqrp.h"
  20. #include "axqwp.h"
  21. #include "libwp.h"
  22. #include "gutilp.h"
  23. #include "gmiscp.h"
  24. #include "gmainp.h"
  25. #include "libfp.h"
  26. #include "librp.h"
  27. #include "setp.h"
  28. #include "libp.h"
  29. #include "miscp.h"
  30. #include "readp.h"
  31.  
  32. static void get_local_ref_maps(IFILE *, int);
  33. static void put_local_ref_maps(IFILE *, int);
  34. static void relocate_slots_a();
  35. static void relocate_slots_b();
  36. static void overwrite_stub_name(char *);
  37.  
  38. extern Segment    CODE_SEGMENT, DATA_SEGMENT, DATA_SEGMENT_MAIN;
  39. extern IFILE *AXQFILE, *LIBFILE, *AISFILE, *STUBFILE;
  40.  
  41. static Tuple code_slots_syms, data_slots_syms;
  42.  
  43. /* Input/Output of compiler files */
  44.  
  45. int load_unit(char *unit, int tree_is_needed)                    /*;load_unit*/
  46. {
  47.     /*
  48.      * Retrieves the symbol table of the given unit and puts its information
  49.      * into the compilation maps.
  50.      * An AXQ may be read from the library if the unit has not yet been
  51.      * loaded. If the file cannot be opened, or the unit is not found, an
  52.      * error message is printed.
  53.      * BEWARE: the loaded AXQ may contain an unit with the same name as the
  54.      * current one, that must not be loaded, as its symbol table would
  55.      * override the current one.
  56.      */
  57.  
  58.     char    *fname;
  59.     int        file_retrieved;
  60.     Symbol    unit_unam;
  61.     Tuple    decmaps, decscopes, s_info;
  62.     Unitdecl    ud;
  63.  
  64.     fname = lib_unit_get(unit);
  65. #ifdef TRACE
  66.     if (debug_flag) gen_trace(strjoin("load_unit ", unit));
  67. #endif
  68.     if (fname == (char *)0) {
  69.         user_error(strjoin(formatted_name(unit), " not present in library"));
  70.         return FALSE;
  71.     }
  72.     else if (in_aisunits_read(unit)) {
  73.         file_retrieved = TRUE;
  74.     }
  75.     else {
  76.         file_retrieved = 
  77.           (read_ais(fname, FALSE, unit, 0, tree_is_needed) != (char *)0);
  78.  
  79.         if (is_subunit(unit)) read_stub(lib_unit_get(unit), unit, "st2");
  80. #ifdef TBSL
  81.         if (is_subunit(unit)) {
  82.             /* If the subunit  has been  compiled, its  stub environment 
  83.                 * overrides the one appearing in the axq of the parent unit.
  84.              */
  85.             (for [n, env] in axqt) STUB_ENV(n) : = env; 
  86.             end ;
  87.         }
  88.         else {
  89.             STUB_ENV +: = axqt;
  90.         }
  91. #endif
  92.     }
  93.  
  94.     if (file_retrieved && (ud = unit_decl_get(unit)) != (Unitdecl)0) {
  95.         /* [unit_unam, s_info, decls] = UNIT_DECL(unit); */
  96.         unit_unam = ud->ud_unam;
  97.         s_info = ud->ud_symbols;
  98.         decscopes = ud->ud_decscopes;
  99.         decmaps = ud->ud_decmaps;
  100.         /* TBSL does the info from decscopes and decmaps need to be restored 
  101.          * or is the info restored by symtab_restore since declared info is 
  102.          * stored with the symbols.
  103.          * DECLARED  += decls; 
  104.          * SYMBTABQ restore 
  105.          */
  106.         symtab_restore(s_info);
  107.         return TRUE;
  108.     }
  109.     else {
  110.         user_error(strjoin("Cannot retrieve unit ", formatted_name(unit)));
  111.         user_info(strjoin(" from file ", fname));
  112.         return FALSE;
  113.     }
  114. }
  115.  
  116.  
  117. void load_library(Axq axq)                                    /*;load_library*/
  118. {
  119.     /*
  120.      * retrieve information from LIBFILE
  121.      * Called only not newlib.
  122.      */
  123.  
  124.     int        comp_status, si, i, j, n, m, unumber, nodes, symbols, cur_level;
  125.     int        parent, unit_count;
  126.     Tuple    stubtup, tup;
  127.     char    *parent_name, *uname, *aisname, *tmp_str, *compdate;
  128.     Set        precedes;
  129.     int        n_code_slots, n_data_slots, n_exception_slots;
  130.     long    cde_pos; /* offset for start of slot info */
  131.     IFILE    *ifile;
  132.  
  133.     ifile = LIBFILE;
  134.     /* library already opened */
  135.     unit_count = getnum(ifile, "lib-unit-count");
  136.     n = getnum(ifile, "lib-n");
  137.     empty_unit_slots = getnum(ifile, "lib-empty-slots");
  138.     tmp_str = getstr(ifile, "lib-tmp-str");
  139.     unit_number_expand(n);
  140.     for (i = 1; i <= unit_count; i++) {
  141.         struct unit *pUnit;
  142.         uname = getstr(ifile, "lib-unit-name");
  143.         unumber = getnum(ifile, "lib-unit-number");
  144.         aisname = getstr(ifile, "lib-ais-name");
  145.         compdate = getstr(ifile, "comp-date");
  146.         symbols = getnum(ifile, "lib-symbols");
  147.         nodes = getnum(ifile, "lib-nodes");
  148.         pUnit = pUnits[unumber];
  149.         pUnit->name = strjoin(uname, "");
  150.         pUnit->isMain = getnum(ifile, "lib-is-main");
  151.         pUnit->libInfo.fname = strjoin(aisname, "");
  152.         pUnit->libInfo.compDate = compdate;
  153.         comp_status = getnum(ifile, "lib-status");
  154.         pUnit->libInfo.obsolete = (comp_status) ? "ok" : "$D$";
  155.         pUnit->libUnit = (comp_status) ? strjoin(uname, "") : "$D$";
  156.         pUnit->aisInfo.numberSymbols = symbols;
  157.         pUnit->treInfo.nodeCount = nodes;
  158.         pUnit->treInfo.tableAllocated = (char *) tup_new(0);
  159.     }
  160.     n = getnum(ifile, "lib-n");
  161.     for (i = 1; i <= n; i++) {
  162.         uname = getstr(ifile, "lib-unit-name");
  163.         aisname = getstr(ifile, "lib-ais-name");
  164.         lib_stub_put(uname, aisname);
  165.         parent = getnum(ifile, "lib-parent");
  166.         if (parent == 0) parent_name = " ";
  167.         else parent_name = pUnits[parent]->name;
  168.         stub_parent_put(uname, parent_name);
  169.         cur_level = getnum(ifile, "lib-cur-level");
  170.         current_level_put(uname, cur_level);
  171.         si = stub_numbered(uname);
  172.         stubtup = (Tuple) stub_info[si];
  173.         m = getnum(ifile, "stub-file-size");
  174.         tup = tup_new(m);
  175.         for (j = 1; j <= m; j++)
  176.             tup[j] = (char *) getnum(ifile, "stub-files");
  177.         stubtup[4] = (char *) tup;
  178.     }
  179.     n = getnum(ifile, "precedes-map-size");
  180.     PRECEDES_MAP = tup_new(n);
  181.     for (i = 1; i <= n; i += 2) {
  182.         PRECEDES_MAP[i] = (char *) getnum(ifile, "precedes-map-ent");
  183.         m = getnum(ifile, "precedes-map-set-size");
  184.         precedes = set_new(m);
  185.         for (j = 1; j <= m; j++) {
  186.             precedes = set_with(precedes,
  187.               (char *) getnum(ifile, "precedes-map-ent"));
  188.         }
  189.         PRECEDES_MAP[i+1] = (char *) precedes;
  190.     }
  191.     n = getnum(ifile, "compilation_table_size");
  192.     compilation_table = tup_new(n);
  193.     for (i = 1; i <= n; i++)
  194.         compilation_table[i] = (char *) getnum(ifile, "compilation-table-ent");
  195.     /* late_instances */
  196.     n = getnum(ifile, "late-instances-size");
  197.     late_instances = tup_new(n);
  198.     for (i = 1; i <= n; i++)
  199.         late_instances[i] = getstr(ifile, "late-instances-str");
  200.     n = getnum(ifile, "interfaced-procedures-size");
  201.     interfaced_procedures = tup_new(n);
  202.     for (i = 1; i <= n; i += 2) {
  203.         interfaced_procedures[i] =
  204.           (char *) getnum(ifile, "interfaced-procedures-num");
  205.         interfaced_procedures[i+1]= getstr(ifile, "interfaced-procedures-str");
  206.     }
  207.     interface_counter = getnum(ifile, "interface-counter");
  208.     n = getnum(ifile, "units-size");
  209.     for (i = 1; i <= n; i++) {
  210.         pUnits[i]->libInfo.currCodeSeg =
  211.           (char *) getnum(ifile, "current-code-seg");
  212.     }
  213.     n = getnum(ifile, "units-size");
  214.     /* read local_reference_map for each unit (tuple of symbols and offsets) */
  215.     get_local_ref_maps(LIBFILE, n);
  216.     cde_pos = get_cde_slots(LIBFILE, axq);
  217.     /* Now set CODE_SLOTS, DATA_SLOTS and EXCEPTION_SLOTS from axq */
  218.     n_code_slots = axq->axq_code_slots_dim -1;
  219.     n_data_slots = axq->axq_data_slots_dim - 1;
  220.     n_exception_slots = axq->axq_exception_slots_dim - 1;
  221.     CODE_SLOTS = tup_new(n_code_slots);
  222.     for (i = 1; i <= n_code_slots; i++) {
  223.         CODE_SLOTS[i] = (char *) axq->axq_code_slots[i];
  224.     }
  225.     DATA_SLOTS = tup_new(n_data_slots);
  226.     for (i = 1; i <= n_data_slots; i++) {
  227.         DATA_SLOTS[i] = (char *) axq->axq_data_slots[i];
  228.     }
  229.     EXCEPTION_SLOTS = tup_new(n_exception_slots);
  230.     for (i = 1; i <= n_exception_slots; i++) {
  231.         EXCEPTION_SLOTS[i] = (char *) axq->axq_exception_slots[i];
  232.     }
  233.     /* could free axq_data_slots, etc., but keep for now */
  234.     /* read out LIB_STUB map (always empty for now) */
  235.     ifclose(LIBFILE);
  236.     return;
  237. }
  238.  
  239. void store_axq(IFILE *file, int unit_num)                        /*;store_axq*/
  240. {
  241.     /* Writes the AXQ file of compiled units (symmetrical to LOAD_AIS) */
  242.  
  243.     int        si, i, n, symbols, slots_ind, nsegs;
  244.     long    begpos;
  245.     Tuple    u_slots, symtup, tup;
  246.     Symbol    sym;
  247.     Segment    seg;
  248.     Fortup    ft1;
  249.     Forset    fs1;
  250.     char    *uname;
  251.     Stubenv    ev;
  252.     IFILE    *ofile;
  253.  
  254. #ifdef TRACE
  255.     if (debug_flag) gen_trace_string("STORE_AXQ: ", pUnits[unit_num]->name);
  256. #endif
  257.  
  258.     /* In order to make the sequence of symbols written out dense (consecutive)
  259.      * without holes, the new symbols which are needed externally, namely 
  260.      * GENERATED_OBJECTS have their seq numbers renumbed before being written
  261.      * out. This new ordering begins right after the sequence number of the last
  262.      * symbol read in from the semantic phase.
  263.      */
  264.     pUnits[unit_num]->libInfo.compDate = (char *)greentime